﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Jerry.DroidBuilder.ThemeEditor.Converters
{
    public class AlignmentValueConverter : IValueConverter, IMultiValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var orientation = parameter as string;
            var val = (value as string) ?? string.Empty;
            var ary = new List<string>(val.Split('|'));

            //center|fill_horizontal|center_vertical|center_horizontal|clip_horizontal|fill|bottom|clip_vertical|left|right|fill_vertical|top

            if (orientation == "v")
            {
                var alignment = VerticalAlignment.Top;

                if (ary.Contains("center") || ary.Contains("center_vertical"))
                {
                    alignment = VerticalAlignment.Center;
                }

                if (ary.Contains("bottom"))
                {
                    alignment = VerticalAlignment.Bottom;
                }

                if (ary.Contains("fill") || ary.Contains("fill_vertical"))
                {
                    alignment = VerticalAlignment.Stretch;
                }

                return alignment;
            }
            else
            {
                var alignment = HorizontalAlignment.Left;

                if (ary.Contains("center") || ary.Contains("center_horizontal"))
                {
                    alignment = HorizontalAlignment.Center;
                }

                if (ary.Contains("right"))
                {
                    alignment = HorizontalAlignment.Right;
                }

                if (ary.Contains("fill") || ary.Contains("fill_horizontal"))
                {
                    alignment = HorizontalAlignment.Stretch;
                }

                return alignment;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var orientation = parameter as string;

            if (orientation == "v")
            {
                var alignment = VerticalAlignment.Top;
                var layoutHeight = values[1] as string;

                if (layoutHeight == "match_parent" || layoutHeight == "fill_parent")
                {
                    alignment = VerticalAlignment.Stretch;
                }
                else
                {
                    alignment = (VerticalAlignment)Convert(values[0], targetType, parameter, culture);
                }

                return alignment;
            }
            else
            {
                var alignment = HorizontalAlignment.Left;
                var layoutWidth = values[1] as string;

                if (layoutWidth == "match_parent" || layoutWidth == "fill_parent")
                {
                    alignment = HorizontalAlignment.Stretch;
                }
                else
                {
                    alignment = (HorizontalAlignment)Convert(values[0], targetType, parameter, culture);
                }

                return alignment;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
